package com.example.endproject;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    SQLiteDatabase mSqlDB;
    DBHelper dbHelper;
    TextView userNameView , pwdView ,subjectView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new DBHelper(this);
        mSqlDB = dbHelper.getWritableDatabase();
        userNameView = findViewById(R.id.UserName);
        pwdView = findViewById(R.id.Password);
        subjectView = findViewById(R.id.main_subject);
        subjectView.setTypeface(Typeface.createFromAsset(getAssets(),"font/FZBangSXJW.ttf"));
    }
    public void submit(View view){
        String userName = userNameView.getText().toString();
        String pwd = pwdView.getText().toString();
        if (isManager(userName,pwd)){//是管理员
            Intent intent = new Intent(this,ManagerActivity.class);
            startActivity(intent);
            Log.i(TAG, "submit: 是管理员账户");
        }
        else if (authentic(userName, pwd)){//成功查询
            Intent intent = new Intent(this,HomeActivity.class);
            intent.putExtra("userName",userName);
            startActivity(intent);
            Log.i(TAG, "submit: success");

        }
        else{//查询失败
            pwdView.setText("");
            Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
            Log.i(TAG, "submit: fail");
        }

    }
    public void register(View view){
        Intent intent = new Intent(this,RegisterActivity.class);
        startActivity(intent);
    }

    public boolean isManager(String username,String pwd){
        return username.equals("0000") && pwd.equals("0000");
    }

    public boolean authentic(String username,String pwd){//参数可以为空
        Cursor query = mSqlDB.query(DBHelper.TB_NAME_USER, new String[]{"user_name", "pwd"},
                "user_name=? AND pwd=?", new String[]{username, pwd}, null, null, null);
        //成功查找到
        boolean flag = query.moveToFirst();
        query.close();
        return flag;
    }
}